package com.achobeta.recruitment.service.impl.wx;

import com.achobeta.recruitment.button.Button;
import com.achobeta.recruitment.button.ext.SubButton;
import com.achobeta.recruitment.service.wx.ButtonService;
import com.achobeta.recruitment.utils.TokenUtils;
import com.achobeta.recruitment.utils.ButtonUtils;
import com.achobeta.recruitment.utils.HttpUtils;
import com.achobeta.recruitment.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-26
 * Time: 22:51
 */
@Service
public class ButtonServiceImpl implements ButtonService {

    @Value("${wx.appid}")
    private String appId;

    @Value("${wx.secret}")
    private String secret;

    @Value("${redirect.fe}")
    private String redirectFE;

    @Value("${recruit.index.url}")
    private String index;


    @Override
    public void createButtonModel() {
        Button button = ButtonUtils.createButton();
        // 一级菜单的两个按钮
        button.getButton().add(ButtonUtils.createViewButton("报名😋", redirectFE + index));
        button.getButton().add(ButtonUtils.createClickButton("简历填写😋", "1"));
        // 二级菜单
        SubButton subButton = ButtonUtils.createSubButton("更多😊");
        subButton.getSub_button().add(ButtonUtils.createViewButton("AchoBeta招新推文", "https://mp.weixin.qq.com/s/_wtaYIslpAWoeiM2mKJWVw"));
        subButton.getSub_button().add(ButtonUtils.createViewButton("知识星球就差一个你！", "https://mp.weixin.qq.com/s/JDeZ1YHRgq7CG6QZCmZOYQ"));
        subButton.getSub_button().add(ButtonUtils.createViewButton("互联网职位与技能简介", "https://mp.weixin.qq.com/s/DbA6WyNbqt73qd3rA8BD0g"));
        subButton.getSub_button().add(ButtonUtils.createClickButton("专属二维码", "2"));
        subButton.getSub_button().add(ButtonUtils.createClickButton("快来帮我们宣传啦", "3"));
        button.getButton().add(subButton);
        // 解析
        String json = JsonUtils.objectToJson(button);
        // 构造url
        String url = " https://api.weixin.qq.com/cgi-bin/menu/create" + HttpUtils.getQueryString(new HashMap<String, Object>() {{
            this.put("access_token", TokenUtils.getToken(appId, secret));
        }});
        // 发送post请求
        String response = HttpUtils.doPost(url, json);
        System.out.println(response);
    }
}
